import os
import argparse
from app import create_app

def parse_arguments():
    parser = argparse.ArgumentParser(description='文档上传管理系统')
    parser.add_argument('--host', default='0.0.0.0', help='监听地址')
    parser.add_argument('--port', type=int, default=5001, help='监听端口')
    parser.add_argument('--env', default='production', choices=['development', 'production'], help='运行环境')
    return parser.parse_args()

def init_directories():
    """初始化必要的目录"""
    directories = ['uploads', 'sessions', 'logs']
    for directory in directories:
        os.makedirs(directory, exist_ok=True)

def main():
    args = parse_arguments()
    os.environ['FLASK_ENV'] = args.env
    
    # 初始化目录
    init_directories()
    
    # 创建并启动应用
    app = create_app()
    
    if args.env == 'development':
        app.run(host=args.host, port=args.port, debug=True)
    else:
        from waitress import serve
        print(f"Starting server on {args.host}:{args.port}")
        serve(app, host=args.host, port=args.port)

if __name__ == '__main__':
    main() 